{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d4e3204a",
   "metadata": {},
   "source": [
    "## 0. 前置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85c6348e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9c0dfa1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "ar_2d=np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])#参数是列表或者元组，而且是矩形的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "860d5494",
   "metadata": {},
   "source": [
    "## 1. 属性property"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db023004",
   "metadata": {},
   "source": [
    "* shape属性：形状,tuple\n",
    "* size 属性： 元素个数  int\n",
    "* ndim属性：维度， int\n",
    "* dtypes属性： 元素数据类型, str\n",
    "* itemsize 属性： 元素字节数  int\n",
    "* T属性： 转置，ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1ab0f79b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "#shape\n",
    "print(ar_2d.shape)\n",
    "ar_2d.shape=(2,6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15ca0d6c",
   "metadata": {},
   "source": [
    "## 2. 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "92e82326",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 50 60]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1, 8, 7])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar_1d=np.array([10,20,50,60,70,80,90,])\n",
    "list_pos=[0,2,3] #位置组合\n",
    "print(ar_1d[list_pos])\n",
    "#ar_2d[行， 列]\n",
    "ar_2d[[0,1,1],(0,3,2)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bae53f8",
   "metadata": {},
   "source": [
    "## 3. 创建/生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d9888e0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n",
      "[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]\n",
      "[[0.63394723 0.52036827 0.87907705]\n",
      " [0.76995709 0.92839223 0.37728317]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 3],\n",
       "       [4, 2, 2]], dtype=int32)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(np.arange(0,1,0.1))\n",
    "print(np.linspace(0,1,11))\n",
    "print(np.random.random([2,3]))#创建多维数组时，必须用元组或列表作为参数。生成随机数[0,1)\n",
    "np.random.randint(2,6,size=(2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71393772",
   "metadata": {},
   "source": [
    "## 4. 排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06f1a28c",
   "metadata": {},
   "source": [
    "### 4.1 shuffle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "580aa7c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.random.shuffle(a) 随机打乱数组 a， 返回None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb8fd72b",
   "metadata": {},
   "source": [
    "### 4.2 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec003e0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ar_2.sort(axis=1)  #axis=1横向排序\n",
    "# ar_2.sort(axis=0)  #axis=0纵向排序\n",
    "# axis是表示被操作的东西，这个东西的值会删除，会变大变小\n",
    "# 对于df，axis=0表示行，axis=1表示列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44a4db23",
   "metadata": {},
   "source": [
    "### 4.3 去重,unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b68d763a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ar_3_uniq=np.unique(ar_3) #去重"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83d3e82e",
   "metadata": {},
   "source": [
    "## 5. 统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5ec9bc7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# a.sum()\n",
    "# a.mean()\n",
    "# a.max()\n",
    "# a.min()\n",
    "# a.vars() #方差\n",
    "# a.std() #标准差\n",
    "# a.argmax() #返回最大值索引\n",
    "# a.argmin() #返回最小值索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb143eed",
   "metadata": {},
   "source": [
    "## 6. 算数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "665820e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False, False])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#与常数，就是对于每个元素都进行运算\n",
    "#与shape一样的数组，就是对于两个元素进行运算\n",
    "np.array([0,1,2,3]) < np.array([2,2,2,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75159b96",
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.logical_or(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "6b18c6f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.add保持一致，与+\n",
    "# 数组点积np.dot(ar_1,ar_2)\n",
    "# 如果是2行3列，与1行3列相乘，则结果为2行3列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc68cacd",
   "metadata": {},
   "source": [
    "### 6.1 矩阵操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "81725a40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a4e6fc16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones([3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d4da2202",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  0,  0,  0],\n",
       "       [ 0,  5,  0,  0],\n",
       "       [ 0,  0,  1,  0],\n",
       "       [ 0,  0,  0, -3]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对角线diagonal_line\n",
    "np.diag([2,5,1,-3],k=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4af4bb17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.17128426, -0.24657369,  1.26174555,  2.00900331,  0.59679304,\n",
       "        -1.91138141, -1.75575031, -1.13168465],\n",
       "       [-1.35761393, -0.10073721,  1.05109419,  0.98758268,  0.1648065 ,\n",
       "         0.64603334,  0.01528739, -0.53866102]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正态分布, scale是标准差（σ）\n",
    "np.random.normal(loc=0.0,scale=1.0,size=[2,8])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b363beeb",
   "metadata": {},
   "source": [
    "## 7. 文件化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d9650128",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  4  5]\n",
      " [ 6  7  7  8  9 10]]\n",
      "NpzFile '../src/data/ar_2d_3.npz' with keys: arr_0, arr_1\n",
      "[[ 1.  2.  3.  4.  4.  5.]\n",
      " [ 6.  7.  7.  8.  9. 10.]]\n"
     ]
    }
   ],
   "source": [
    "ar_3=np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])\n",
    "np.save(\"../src/data/ar_2d.npy\",ar_2d) #存为.npy文件\n",
    "np.savez(\"../src/data/ar_2d_3.npz\",ar_2d, ar_3)\n",
    "np.savetxt('../src/data/ar_2d.txt',ar_2d,fmt='%6.2f',delimiter=',')\n",
    "print(np.load(\"../src/data/ar_2d.npy\"))\n",
    "print(np.load(\"../src/data/ar_2d_3.npz\"))\n",
    "print(np.loadtxt('../src/data/ar_2d.txt',delimiter=','))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "cc2dfd9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['arr_0', 'arr_1']\n"
     ]
    }
   ],
   "source": [
    "print(np.load(\"../src/data/ar_2d_3.npz\").files)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cef0dcf6",
   "metadata": {},
   "source": [
    "## 10. 同等变换\n",
    "### 10.1 展平/堆叠/分割,flatten,hstack,hsplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c5e494d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  4  5  6  7  7  8  9 10]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  4,  5,  6,  7,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(ar_2d.ravel())\n",
    "ar_2d.flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "10f407ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "#ar_2d_3x9=np.hstack( (ar_2d_3x4,ar_2d_3x5) )\n",
    "#ar_2d_5x4=np.vstack( (ar_2d_3x4,ar_2d_2x4) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "07180777",
   "metadata": {},
   "outputs": [],
   "source": [
    "# list_1=np.hsplit(ar_2d_4x8,2)\n",
    "# list_2=np.vsplit(ar_2d_4x8,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b277553",
   "metadata": {},
   "source": [
    "### 10.2 维度调整transpose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dab7fd8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ar_chw=np.transpose(ar_hwc,(2,1,0))\n",
    "# ar_chw_rgb=ar_chw_bgr[::-1,:,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a08768f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mining",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
